/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cronos.agent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Leonardo Oliveira Moreira
 *
 * Class used to interact with operating system commands
 *
 * OSs Supported: - Linux (Total) - Windows (Partial) DBMSs Supported: - MySQL
 * (Total) - PostgreSQL (Total)
 */
public final class ShellCommand {

    public static final int LINUX = 1;
    public static final int WINDOWS = 2;

    /**
     * Prevents an object be created (Static Class)
     */
    private ShellCommand() {
    }

    /**
     * Method used to check what operating system is installed in this virtual
     * machine
     *
     * @return - A constant that indicates the operating system
     */
    public static int getOperationSystem() {
        String operationalSystem = System.getProperty("os.name");
        if (operationalSystem.equalsIgnoreCase(("linux"))) {
            return LINUX;
        } else {
            return WINDOWS;
        }
    }

    /**
     * Method used to get the percentage of the CPU free in your operation
     * system
     *
     * @return
     */
    public static double getCPUFreePercentage() {
        double result = 0;
        ProcessBuilder process = new ProcessBuilder(new String[]{"bash", "-c", "vmstat | awk '{print $15}' | tail -1"});
        try {
            Process p = process.start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String string = "";
            int l = 0;
            while ((string = stdInput.readLine()) != null) {
                if (l == 0 && string != null) {
                    return Double.parseDouble(string);
                }
                l++;
            }
        } catch (Exception ex) {
            return result;
        }
        return result;
    }

    /**
     * Method used to get the total memory avaliable in your operation system
     *
     * @return
     */
    public static long getMemoryTotal() {
        long result = 0;
        ProcessBuilder process = new ProcessBuilder(new String[]{"bash", "-c", "free -m | grep Mem | awk '{print $2}'"});
        try {
            Process p = process.start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String string = "";
            int l = 0;
            while ((string = stdInput.readLine()) != null) {
                if (l == 0 && string != null) {
                    return Long.parseLong(string);
                }
                l++;
            }
        } catch (Exception ex) {
            return result;
        }
        return result;
    }

    /**
     * Method used to get the free memory avaliable in your operation system
     *
     * @return
     */
    public static long getMemoryFree() {
        long result = 0;
        ProcessBuilder process = new ProcessBuilder(new String[]{"bash", "-c", "free -m | grep Mem | awk '{print $4}'"});
        try {
            Process p = process.start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String string = "";
            int l = 0;
            while ((string = stdInput.readLine()) != null) {
                if (l == 0 && string != null) {
                    return Long.parseLong(string);
                }
                l++;
            }
        } catch (Exception ex) {
            return result;
        }
        return result;
    }

    /**
     * Method used to get the total storage disk avaliable in your operation
     * system
     *
     * @return
     */
    public static long getDiskTotal() {
        long result = 0;
        ProcessBuilder process = new ProcessBuilder(new String[]{"bash", "-c", "df -v . | awk '{print $2}' | tail -1"});
        try {
            Process p = process.start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String string = "";
            int l = 0;
            while ((string = stdInput.readLine()) != null) {
                if (l == 0 && string != null) {
                    return Long.parseLong(string);
                }
                l++;
            }
        } catch (Exception ex) {
            return result;
        }
        return result;
    }

    /**
     * Method used to get the free storage disk avaliable in your operation
     * system
     *
     * @return
     */
    public static long getDiskFree() {
        long result = 0;
        ProcessBuilder process = new ProcessBuilder(new String[]{"bash", "-c", "df -v . | awk '{print $4}' | tail -1"});
        try {
            Process p = process.start();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String string = "";
            int l = 0;
            while ((string = stdInput.readLine()) != null) {
                if (l == 0 && string != null) {
                    return Long.parseLong(string);
                }
                l++;
            }
        } catch (Exception ex) {
            return result;
        }
        return result;
    }

    public static File downloadFile(String strUrl, String fileName) {
        File resultFile = null;
        try {
            URL url = new URL(strUrl);
            InputStream inputStream = url.openStream();
            FileOutputStream outputStream = new FileOutputStream(fileName);
            int i = 0;  
            byte[] buffer = new byte[1024];  
            while ((i = inputStream.read(buffer)) >= 0) {  
                outputStream.write(buffer, 0, i);  
            } 
            outputStream.close();
            inputStream.close();
            resultFile = new File(fileName);
        } catch (IOException ex) {
        }
        return resultFile;
    }

    public static void main(String[] args) {
    }
}